#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<int> smallestK(vector<int>& arr, int k) {
        srand(time(NULL));
        qsort(arr, 0, arr.size() - 1, k);
        return { arr.begin(), arr.begin() + k };
    }
    void qsort(vector<int>& arr, int l, int r, int k)
    {
        if (l >= r) return;
        int key = getRandom(arr, l, r);
        int i = l, left = l - 1, right = r + 1;
        while (i < right)
        {
            if (arr[i] < key) swap(arr[++left], arr[i++]);
            else if (arr[i] == key) i++;
            else swap(arr[--right], arr[i]);
        }
        int a = left - l + 1, b = (right - 1) - (left + 1) + 1;
        if (a >= k) qsort(arr, l, left, k);
        else if ((a + b) >= k) return;
        else qsort(arr, right, r, k - a - b);
    }
    int getRandom(vector<int>& arr, int left, int right)
    {
        return arr[((rand() % (right - left + 1))) + left];
    }
};